﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using YuuzleComic.DBUtility;
using System.Data;
using System.Text;
using YuuzleComic.Common;
using Newtonsoft.Json.Linq;
using System.Net;

public partial class yzadmin_makehtml_index_page : YuuzleComic.Web.UI.ManagePage
{
    protected string zzlist;
    protected string today;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            JArray todayObj = new JArray();
            List<string> zjid = new List<string>();
            List<string> comicid = new List<string>();
            List<string> zzid = new List<string>();
            List<string> classid = new List<string>();
            List<string> dqid = new List<string>();
            List<string> zmid = new List<string>();
            DataSet ds = DbHelperSQL.Query("select id from zj where DateDiff(d,time,GETDATE())=(select DateDiff(d,MAX(time),GETDATE()) from zj)");
            foreach (DataRow dr in ds.Tables[0].Rows)
	        {
		        zjid.Add(dr["id"].ToString());
	        }
            todayObj.Add(JObject.Parse("{id:["+string.Join(",", zjid.ToArray())+"],name:'章节',url:'/yzadmin/data/makehtml.aspx?a=zj&id='}"));
            ds = DbHelperSQL.Query("select distinct cid from zj where DateDiff(d,time,GETDATE())=(select DateDiff(d,MAX(time),GETDATE()) from zj)");
            foreach (DataRow dr in ds.Tables[0].Rows)
	        {
		        comicid.Add(dr["cid"].ToString());
	        }
            todayObj.Add(JObject.Parse("{id:[" + string.Join(",", comicid.ToArray()) + "],name:'漫画',url:'/yzadmin/data/makehtml.aspx?a=comic&id='}"));
            ds = DbHelperSQL.Query("select distinct zzid from comics_zz where cid in ("+string.Join("," ,comicid.ToArray())+")");
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                zzid.Add(dr["zzid"].ToString());
            }
            todayObj.Add(JObject.Parse("{id:[" + string.Join(",", zzid.ToArray()) + "],name:'作者',url:'/yzadmin/data/makehtml.aspx?a=author&id='}"));
            ds = DbHelperSQL.Query("select distinct classid from comics where id in (" + string.Join(",", comicid.ToArray()) + ")");
            foreach (DataRow dr in ds.Tables[0].Rows)
	        {
                classid.Add(dr["classid"].ToString());
	        }
            todayObj.Add(JObject.Parse("{id:[" + string.Join(",", classid.ToArray()) + "],name:'类别',url:'/yzadmin/data/makehtml.aspx?a=class&classid='}"));
            ds = DbHelperSQL.Query("select distinct dqid from comics where id in ("+string.Join("," ,comicid.ToArray())+")");
            foreach (DataRow dr in ds.Tables[0].Rows)
	        {
                dqid.Add(dr["dqid"].ToString());
	        }
            todayObj.Add(JObject.Parse("{id:[" + string.Join(",", dqid.ToArray()) + "],name:'地区',url:'/yzadmin/data/makehtml.aspx?a=diqu&classid='}"));
            ds = DbHelperSQL.Query("select distinct zm from comics where id in (" + string.Join(",", comicid.ToArray()) + ")");
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                zmid.Add(dr["zm"].ToString());
            }
            todayObj.Add(JObject.Parse("{id:['" + string.Join("','", zmid.ToArray()) + "'],name:'字母',url:'/yzadmin/data/makehtml.aspx?a=abcd&abcd='}"));
            today = todayObj.ToString();

            ds = DbHelperSQL.Query("select a.id,a.mhmc,a.mhfm,a.zm,a.zt,c.id zjid,c.zjmc from comics a join (select MAX(ordernum) ordernum,cid from zj group by cid) b on a.id=b.cid join zj c on b.ordernum=c.ordernum order by a.mhmc");
            if (ds.Tables[0].Rows.Count > 0)
            {
                string html = Input.ReadTextFile("~/admin/temp/all.html");
                html = Input.CommonReplace(html);

                html = html.Replace("[WEB_NAME]", "雨泽漫画");
                html = html.Replace("[WEB_URL]", "http://www.yzmanga.com/");

                string wanjie = html.Replace("[TITLE]", "完结").Replace("[ETITLE]", "wanjie");
                wanjie = wanjie.Replace("[list]", GetList(ds.Tables[0], " and zt=1"));
                Input.WriteTextFile("~/list/wanjie.html", wanjie);
                string lianzai = html.Replace("[TITLE]", "连载").Replace("[ETITLE]", "lianzai");
                lianzai = lianzai.Replace("[list]", GetList(ds.Tables[0], " and zt=0"));
                Input.WriteTextFile("~/list/lianzai.html", lianzai);
                string all = html.Replace("[TITLE]", "全部").Replace("[ETITLE]", "all");
                all = all.Replace("[list]", GetList(ds.Tables[0], ""));
                Input.WriteTextFile("~/list/all.html", all);

                html = Input.ReadTextFile("~/admin/temp/authorlist.html");
                html = Input.CommonReplace(html);
                html = html.Replace("[WEB_NAME]", "雨泽漫画");
                html = html.Replace("[WEB_URL]", "http://www.yzmanga.com/");
                html = html.Replace("[list]", GetAuthorList());
                Input.WriteTextFile("~/list/author.html", html);

                html = Input.ReadTextFile("~/admin/temp/update.html");
                html = Input.CommonReplace(html);
                html = html.Replace("[WEB_NAME]", "雨泽漫画");
                html = html.Replace("[WEB_URL]", "http://www.yzmanga.com/");
                html = html.Replace("[list]", GetUpdateList());
                Input.WriteTextFile("~/list/update.html", html);

                html = Input.ReadTextFile("~/admin/temp/new.html");
                html = Input.CommonReplace(html);
                html = html.Replace("[WEB_NAME]", "雨泽漫画");
                html = html.Replace("[WEB_URL]", "http://www.yzmanga.com/");
                html = html.Replace("[list]", GetNewList());
                Input.WriteTextFile("~/list/new.html", html);

                html = Input.ReadTextFile("~/admin/temp/top.html");
                html = Input.CommonReplace(html);
                html = html.Replace("[WEB_NAME]", "雨泽漫画");
                html = html.Replace("[WEB_URL]", "http://www.yzmanga.com/");
                html = html.Replace("[list]", GetTopList());
                Input.WriteTextFile("~/list/top.html", html);
            }
            Response.Write("生成成功");
        }
    }

    private string GetNewList()
    {
        StringBuilder sb = new StringBuilder();
        DataSet ds;
        ds = DbHelperSQL.Query("select top(200) a.id,a.mhmc,a.zt,a.adddate,c.id zjid,c.zjmc,c.time,STUFF((select ',' +cast(zzid as varchar) from comics_zz where cid=a.id FOR XML PATH('')), 1, 1, '') zzid,STUFF((SELECT ','+ z.zzmc FROM zz z join comics_zz cz on z.id=cz.zzid where cz.cid=a.id FOR XML PATH('')), 1, 1, '') zzmc from comics a join (select MAX(ordernum) ordernum,cid from zj group by cid) b on a.id=b.cid join zj c on b.ordernum=c.ordernum order by a.id desc");
        sb.Append("<ul>");
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            DataRow dr = ds.Tables[0].Rows[i];
            int days = DateTime.Now.Subtract(Convert.ToDateTime(dr["adddate"])).Days;
            string cls = "";
            if (days == 0)
            {
                cls = "red";
            }
            else if (days == 1)
            {
                cls = "orange";
            }
            if (Convert.ToBoolean(dr["zt"]))
            {
                sb.Append("<li><span class=\"" + cls + "\">" + Convert.ToDateTime(dr["time"]).ToShortDateString() + "</span><em>" + (i + 1) + "</em><a href=\"/comic/" + dr["id"] + "/\" title=\"" + dr["mhmc"] + "\">" + dr["mhmc"] + "</a>[<a class=\"green\" href=\"/comic/" + dr["id"] + "/" + dr["zjid"] + ".html\" title=\"完结\">完结</a>]");
            }
            else
            {
                sb.Append("<li><span class=\"" + cls + "\">" + Convert.ToDateTime(dr["time"]).ToShortDateString() + "</span><em>" + (i + 1) + "</em><a href=\"/comic/" + dr["id"] + "/\" title=\"" + dr["mhmc"] + "\">" + dr["mhmc"] + "</a>[<a class=\"red\" href=\"/comic/" + dr["id"] + "/" + dr["zjid"] + ".html\" title=\"" + dr["zjmc"] + "\">" + dr["zjmc"] + "</a>]");
            }
            string mhzz = Input.CreateAuthorLinks(dr["zzid"].ToString(), dr["zzmc"].ToString());
            foreach (string item in dr["zzmc"].ToString().Split(','))
            {
                mhzz = mhzz.Replace(item, "<acronym>" + item + "</acronym>");
            }
            sb.Append(" " + mhzz + "</li>");
            if (i == 24 || i == 74 || i == 124 || i == 174)
            {
                sb.Append("</ul><ul>");
            }
            else if (i == 49 || i == 99 || i == 149)
            {
                sb.Append("</ul><div class=\"dot-line\"></div><ul>");
            }
        }
        sb.Append("</ul>");
        return sb.ToString();
    }

    private string GetTopList()
    {
        StringBuilder sb = new StringBuilder();
        YuuzleComic.BLL.diqu diqubll = new YuuzleComic.BLL.diqu();
        YuuzleComic.BLL.ycclass classbll = new YuuzleComic.BLL.ycclass();
        sb.Append(this.GetTopHtml("总阅读", 15, "1=1", "zjhits"));
        sb.Append(this.GetTopHtml("总点击", 15, "1=1", "hits"));
        sb.Append(this.GetTopHtml("月点击", 15, "1=1", "hits_m"));
        sb.Append(this.GetTopHtml("周点击", 15, "1=1", "hits_w"));
        sb.Append(this.GetTopHtml("日点击", 15, "1=1", "hits_d"));
        sb.Append("<div class=\"blank4\"></div><div class=\"blank8\"></div>");
        DataSet ds = diqubll.GetAllList();
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            sb.Append(this.GetTopHtml(dr["diqu"] + "漫画", 15, "dqid=" + dr["id"], "hits"));
        }
        sb.Append("<div class=\"blank4\"></div><div class=\"blank8\"></div>");
        ds = classbll.GetAllList();
        for (int i = 1; i <= ds.Tables[0].Rows.Count; i++)
        {
            DataRow dr = ds.Tables[0].Rows[i - 1];
            sb.Append(this.GetTopHtml(dr["classname"] + "漫画", 15, "classid=" + dr["id"], "hits"));
            if (i == 5)
            {
                sb.Append("<div class=\"blank4\"></div><div class=\"blank8\"></div>");
            }
        }
        return sb.ToString();
    }

    private string GetUpdateList()
    {
        StringBuilder sb = new StringBuilder();
        DataSet ds;
        ds = DbHelperSQL.Query("select top(200) a.id,a.mhmc,a.zt,a.adddate,c.id zjid,c.zjmc,c.time,STUFF((select ',' +cast(zzid as varchar) from comics_zz where cid=a.id FOR XML PATH('')), 1, 1, '') zzid,STUFF((SELECT ','+ z.zzmc FROM zz z join comics_zz cz on z.id=cz.zzid where cz.cid=a.id FOR XML PATH('')), 1, 1, '') zzmc from comics a join (select MAX(id) id,cid from zj group by cid) b on a.id=b.cid join zj c on b.id=c.id order by b.id desc");
        sb.Append("<ul>");
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            DataRow dr = ds.Tables[0].Rows[i];
            int days = DateTime.Now.Subtract(Convert.ToDateTime(dr["time"])).Days;
            string cls = "";
            if (days == 0)
            {
                cls = "red";
            }
            else if (days == 1)
            {
                cls = "orange";
            }
            if (Convert.ToBoolean(dr["zt"]))
            {
                sb.Append("<li><span class=\"" + cls + "\">" + Convert.ToDateTime(dr["time"]).ToShortDateString() + "</span><em>" + (i + 1) + "</em><a href=\"/comic/" + dr["id"] + "/\" title=\"" + dr["mhmc"] + "\">" + dr["mhmc"] + "</a>[<a class=\"green\" href=\"/comic/" + dr["id"] + "/" + dr["zjid"] + ".html\" title=\"完结\">完结</a>]");
            }
            else
            {
                sb.Append("<li><span class=\"" + cls + "\">" + Convert.ToDateTime(dr["time"]).ToShortDateString() + "</span><em>" + (i + 1) + "</em><a href=\"/comic/" + dr["id"] + "/\" title=\"" + dr["mhmc"] + "\">" + dr["mhmc"] + "</a>[<a class=\"red\" href=\"/comic/" + dr["id"] + "/" + dr["zjid"] + ".html\" title=\"" + dr["zjmc"] + "\">" + dr["zjmc"] + "</a>]");
            }
            string mhzz = Input.CreateAuthorLinks(dr["zzid"].ToString(),dr["zzmc"].ToString());
            foreach (string item in dr["zzmc"].ToString().Split(','))
	        {
		        mhzz =  mhzz.Replace(item,"<acronym>" + item + "</acronym>");
	        }
            sb.Append(" " + mhzz + "</li>");
            if (i == 24 || i == 74 || i == 124 || i == 174)
            {
                sb.Append("</ul><ul>");
            }
            else if (i == 49 || i == 99 || i == 149)
            {
                sb.Append("</ul><div class=\"dot-line\"></div><ul>");
            }
        }
        sb.Append("</ul>");
        return sb.ToString();
    }

    private string GetList(DataTable dt, string addwhere)
    {
        StringBuilder sb = new StringBuilder();
        string[] zmArr = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
        string[] szArr = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0-9" };
        foreach (string zm in zmArr)
        {
            sb.Append("<A id=\"" + zm + "\" name=\"" + zm + "\"></A>");
            sb.Append("<H3><SPAN><A href=\"#\">返回顶部↑</A></SPAN><EM><A href=\"/list/" + zm + "/\">" + zm + "</A></EM></H3>");
            sb.Append("<UL>");
            foreach (DataRow dr in dt.Select("zm='" + zm + "'" + addwhere, "mhmc"))
            {
                string zuixin = Convert.ToBoolean(dr["zt"]) ? "完结" : dr["zjmc"].ToString();
                sb.Append("<LI><A title=\"" + dr["mhmc"] + "\" href=\"/comic/" + dr["id"] + "/\" rel=\"/cpic/" + dr["mhfm"] + "\" target=\"_blank\">" + dr["mhmc"] + "</A> [<A class=\"" + (Convert.ToBoolean(dr["zt"]) ? "green" : "red") + "\" title=\"" + zuixin + "\" href=\"/comic/" + dr["id"] + "/" + dr["zjid"] + ".html\" target=\"_blank\">" + zuixin + "</A>]</LI>");
            }
            sb.Append("</UL><DIV class=\"blank4\"></DIV>");
        }

        sb.Append("<A id=\"0-9\" name=\"0-9\"></A>");
        sb.Append("<H3><SPAN><A href=\"#\">返回顶部↑</A></SPAN><EM><A href=\"/list/0-9/\">0-9</A></EM></H3>");
        sb.Append("<UL>");
        foreach (DataRow dr1 in dt.Select("zm in ('" + string.Join("','", szArr) + "')" + addwhere, "mhmc"))
        {
            string zuixin = Convert.ToBoolean(dr1["zt"]) ? "完结" : dr1["zjmc"].ToString();
            sb.Append("<LI><A title=\"" + dr1["mhmc"] + "\" href=\"/comic/" + dr1["id"] + "/\" rel=\"/cpic/" + dr1["mhfm"] + "\" target=\"_blank\">" + dr1["mhmc"] + "</A> [<A class=\"" + (Convert.ToBoolean(dr1["zt"]) ? "green" : "red") + "\" title=\"" + zuixin + "\" href=\"/comic/" + dr1["id"] + "/" + dr1["zjid"] + ".html\" target=\"_blank\">" + zuixin + "</A>]</LI>");
        }
        sb.Append("</UL><DIV class=\"blank4\"></DIV>");
        return sb.ToString();
    }

    private string GetAuthorList()
    {
        DataTable dt = DbHelperSQL.Query("select a.id,a.zzmc,a.tj,a.zztp,a.zm,a.dqid,b.mhcount from zz a join (select COUNT(t1.id) mhcount,t2.zzid from comics t1 join comics_zz t2 on t1.id=t2.cid group by t2.zzid) b on a.id=b.zzid").Tables[0];
        zzlist = JArray.FromObject(dt).ToString();
        StringBuilder sb = new StringBuilder();
        YuuzleComic.BLL.diqu diqubll = new YuuzleComic.BLL.diqu();
        foreach (int dqid in new int[] { 1, 2, 4, 5, 3 })
        {
            YuuzleComic.Model.ycdiqu diqu = diqubll.GetModel(dqid);
            sb.Append("<A id=\"" + diqu.filename + "\" name=\"" + diqu.filename + "\"></A>");
            sb.Append("<H3><SPAN><A href=\"#\">返回顶部↑</A></SPAN><EM style=\"width:120px;\"><A href=\"/list/" + diqu.filename + ".html\">" + diqu.diqu + "漫画家</A></EM></H3>");
            sb.Append("<UL>");
            foreach (DataRow dr in dt.Select("dqid=" + dqid, "mhcount desc"))
            {
                sb.Append("<LI><A title=\"" + dr["zzmc"] + "\" href=\"/list/author/" + dr["id"] + ".html\" rel=\"/zpic/" + dr["zztp"] + "\" target=\"_blank\">" + dr["zzmc"] + " [<font color=blue>共" + dr["mhcount"] + "部</font>]</A></LI>");
            }
            sb.Append("</UL><DIV class=\"blank4\"></DIV>");
        }
        return sb.ToString();
    }

    private string GetTopHtml(string name, int topCount, string strWhere, string strOrder)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<div class=\"topHits\"><h2 class=\"bar\"><strong>" + name + "排行</strong></h2><ul>");
        DataSet ds = DbHelperSQL.Query("select top(" + topCount + ") * from comics join (select SUM(hits) zjhits,cid from zj group by cid) t on id=t.cid where " + strWhere + " order by " + strOrder + " desc");
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            DataRow dr = ds.Tables[0].Rows[i];
            sb.Append("<li><span class=\"t" + (i + 1) + "\">" + (i + 1) + ".</span><a href=\"/comic/" + dr["id"] + "/\" target=\"_blank\" title=\"" + dr["mhmc"] + "\">" + dr["mhmc"] + "</a></li>");
        }
        sb.Append("</ul></div>");
        return sb.ToString();
    }
}